import cmd, sys, os

import drectangular,f,analyse

class CLI(cmd.Cmd):

    def __init__(self, base, configdict):
        cmd.Cmd.__init__(self)
        self.prompt = '> '
        self.configdict=configdict        
       

    def do_quit(self, arg):
        sys.exit(1)
    
        
    def do_frename(self, arg):
        arg=string.split(arg,' ')
        f.frename(arg[0],arg[1])        
    
    def do_fcopy(self, arg):
        arg=string.split(arg,' ')
        f.fcopy(arg[0],arg[1])
        
    
    def do_fremove(self, arg):
        f.fremove(arg)
        
    def do_fimport(self, arg):
        print "importing", arg
        print c
    
    def do_fexport(self, arg):
        print "exporting", arg
        
    def do_flist(self, arg):
        f.flist()        
        
    
    def do_drect_start(self, arg):
        global rect
        width = self.configdict.getint('Display Rectangular','Monitor Width')
        height = self.configdict.getint('Display Rectangular','Monitor Height')
        left_border = self.configdict.getint('Display Rectangular','Left Border')
        upper_border =  self.configdict.getint('Display Rectangular','Upper Border')
        dir_incr = self.configdict.getint('Display Rectangular','Dir Net Increment')
        dip_incr = self.configdict.getint('Display Rectangular','Dip Net Increment')
        rect = drectangular.drect(width, height, left_border, upper_border, dip_incr, dir_incr)
        
    def do_drect_draw_line(self, arg):
        rect.draw_lines()
        
    def do_drect_write(self, arg):        
        rect.write_to_file('debian.png')
        
    def do_drect_points(self, arg):
        data=analyse.double_data(f.get_data_list(arg,'fract.txt'))
        rect.draw_points(data)
        
    def do_drect_point(self, arg):
        arg=string.split(arg,' ')
        arg[0]=int(arg[0]); arg[1]=int(arg[1])
        data=analyse.double_data([arg])
        rect.draw_points(data)
        
    def do_drect_density(self, arg):
        size_of_gridwind = self.configdict.getint('Analyse','Size of a Grid Window')
        data=f.get_data_list(arg,'density.txt')
        rect.density_mesh(data,size_of_gridwind)
    
    def do_sdensity(self,arg):
        size_of_gridwind = self.configdict.getint('Analyse','Size of a Grid Window')
        size_of_averwind = self.configdict.getint('Analyse','Size of an Average Window')
        #data=analyse.double_data(f.get_data_list(arg,'fract.txt'))
        data=analyse.double_doubled_data(f.get_data_list(arg,'fract.txt'))
        density=analyse.density_grid(data, size_of_gridwind, size_of_averwind)
        f.save_data_list(density,arg,'density.txt')
        
    def do_sisolines(self,arg):
        size_of_gridwind = self.configdict.getint('Analyse','Size of a Grid Window')
        data=f.get_data_list(arg,'density.txt')        
        coords=analyse.isolines(data)
        rect.draw_lines(coords, size_of_gridwind)
    
    def help_quit(self):
        print "syntax: quit",
        print "-- terminates the application"
        
            
    def help_frename(self):
        print "syntax: frename [old name of a site]",
        print "[new name]"
    
    def help_fcopy(self):
        print "syntax: fcopy [name of a source to copy]",
        print "[name of a new site]"

    # shortcuts
    do_q = do_quit
